package com.xilihui.xlh.business.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xilihui.xlh.R;
import com.xilihui.xlh.business.dialogs.BillScreeningDialog;
import com.xilihui.xlh.business.entities.AccountEntity;
import com.xilihui.xlh.business.requests.MineRequest;
import com.xilihui.xlh.component.recyclerview.BaseAdapter;
import com.xilihui.xlh.component.recyclerview.BaseViewHolder;
import com.xilihui.xlh.core.app.ToolBaseCompatActivity;
import com.xilihui.xlh.core.exception.DefaultExceptionListener;
import com.xilihui.xlh.core.exception.ExceptionManager;
import com.xilihui.xlh.core.http.DoTransform;
import com.xilihui.xlh.core.http.HttpSubscriber;
import com.xilihui.xlh.core.util.ToastUtil;
import com.xilihui.xlh.core.util.YEventBuses;

import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.http.POST;

/**
 * Created by Administrator on 2018/7/4.
 */

public class BillActivity extends ToolBaseCompatActivity implements OnRefreshListener, OnLoadMoreListener, View.OnClickListener {
    
    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    
    int page = 1;
    String type = "10086";
    
    ExceptionManager exceptionManager;
    
    ArrayList<AccountEntity.DataBean> data = new ArrayList<>();
    ArrayList<AccountEntity.StaBean> sta = new ArrayList<>();
    BaseAdapter<AccountEntity.DataBean> beanBaseAdapter;

    BillScreeningDialog billScreeningDialog;
    
    @Override
    public int getLayout() {
        return R.layout.activity_bill;
    }

    @Override
    public String initTitle() {
        return null;
    }

    @OnClick(R.id.iv_toolbar_left)
    public void back(){
        finish();
    }
    
    @OnClick(R.id.tv_toolbar_right)
    public void sift(){
        billScreeningDialog = new BillScreeningDialog(BillActivity.this, 
                sta);
        billScreeningDialog.show();
    }
    
    @Override
    public void initUI(@Nullable Bundle savedInstanceState) {
        setToolTitle("账单");
        setToolRightText("筛选");
        
        exceptionManager = new ExceptionManager(smartRefreshLayout,new DefaultExceptionListener(this)){
            @Override
            protected int getEmptyView() {
                return R.layout.empty_bill;
            }
        };
        
        smartRefreshLayout.setOnRefreshListener(this);
        smartRefreshLayout.setOnLoadMoreListener(this);
        
        beanBaseAdapter = new BaseAdapter<AccountEntity.DataBean>(this,data) {
            @Override
            protected int setLayoutId() {
                return R.layout.recycler_item_bill;
            }

            @Override
            protected void getView(BaseViewHolder holder, final AccountEntity.DataBean entity, int position) {
                
                holder.setText(R.id.tv_name,entity.getProject());
                holder.setText(R.id.tv_time,entity.getAdd_time());
                holder.setText(R.id.tv_num,entity.getMoney());
                
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(BillActivity.this,BillingDetailsActivity.class);
                        intent.putExtra("id",entity.getId());
                        intent.putExtra("type",entity.getType());
                        startActivity(intent);
                    }
                });
            }
        };
        
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(beanBaseAdapter);
    
    }

    @Override
    public void initData() {
        getData(true);
    }
    
    public void getData(boolean isLoading){
        
        MineRequest.accountList(this,page,type)
                .compose(DoTransform.<AccountEntity>applyScheduler(this,isLoading))
                .compose(this.<AccountEntity>bindToLifecycle())
                .subscribe(new HttpSubscriber<AccountEntity>(this) {
                    @Override
                    protected void onSuccess(AccountEntity entity) {
                        smartRefreshLayout.finishRefresh();
                        smartRefreshLayout.finishLoadMore();
                        
                        if(page==1){
                            data.clear();
                            
                            if(sta.size()<1){
                                sta.addAll(entity.getSta());
                                sta.get(0).setSelect(true);
                            }
                            
                            if(entity.getData().size()>0){
                                exceptionManager.hide();
                            }else {
                                exceptionManager.showEmpty();
                            }
                            
                        }
                        
                        data.addAll(entity.getData());
                        beanBaseAdapter.setList(data);
                        
                        if(page>=entity.getPageCount()){
                            smartRefreshLayout.finishLoadMoreWithNoMoreData();
                        }else {
                            smartRefreshLayout.setNoMoreData(false);
                        }
                        
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.toastShortNegative(msg);
                        smartRefreshLayout.finishRefresh();
                        smartRefreshLayout.finishLoadMore();
                    }
                });
        
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        page = 1;
        getData(false);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        page = page + 1;
        getData(false);
    }

    @Subscribe
    public void onEventMainThread(YEventBuses.Event event) {
        super.onEventMainThread(event);
        if(event.is("bill_sift")){
            type = (String) event.getParams(0);
            page = 1;
            getData(true);
            smartRefreshLayout.postDelayed(new Runnable() {
                @Override
                public void run() {
                    billScreeningDialog.dismiss();
                }
            },800);
        }
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View view) {
        
    }
}
